import './public-path'
// with polyfills
import 'core-js/stable'
import 'regenerator-runtime/runtime'
import 'fetch-polyfill'
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import i18n from './locales'
import request, { VueAxios, initRequest } from './utils/request'
import { store as commonStore, tools } from 'crec-common'
import store from './store/'
import initializer from './core/initializer'
import './core/lazy_use' // use lazy load components
import './permission' // permission control
import './utils/filter' // global filter
import './styles/global.less' // global style
// vex-table插件
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
// 工具函数全局
import Utils from './utils/index'
// 引入全局混入
import globalMixin from './mixins/global'

// 引入业务组件
import cb from 'crec-business-component'
import '@/core/directives/drag'

// 引入CREC-UI，统一调整默认样式
import { Card, Table } from 'crec-ui'

// 将card和table在全局的样式设置为small
[Card, Table].forEach((item) => {
  item.props.size.default = 'small'
})

Vue.mixin(globalMixin)
Vue.use(VXETable)
Vue.use(Utils)

Vue.config.productionTip = false

// mount axios to `Vue.$http` and `this.$http`
Vue.use(VueAxios)
Vue.use(cb, { request, serviceName: process.env.VUE_APP_API_BASIC_AUTHORITY_WEB_SERVICE_URL })
// use pro-layout components
let instance = null
function render (props = {}) {
  const { container, routerBase, mode, requestBase } = props
  // 子应用请求代理的base
  initRequest({ requestBase })
  // hash模式需要重新设置路由地址，增加前缀
  if (mode === 'hash') {
    console.log('routerBase', routerBase)
  } else {
    router.options.base = window.__POWERED_BY_QIANKUN__ ? routerBase : process.env.VUE_APP_PUBLIC_PATH
    router.history.base = window.__POWERED_BY_QIANKUN__ ? routerBase : process.env.VUE_APP_PUBLIC_PATH
  }
  instance = new Vue({
    router,
    store,
    i18n,
    // init localstorage, vuex
    created: initializer,
    render: h => h(App)
  }).$mount(container ? container.querySelector(`#${process.env.VUE_APP_NAME}`) : `#${process.env.VUE_APP_NAME}`)
}

if (!window.__POWERED_BY_QIANKUN__) {
  // 这里是子应用独立运行的环境，实现子应用的登录逻辑

  // 独立运行时，也注册一个名为global的store module
  commonStore.globalRegister(store)

  var code = tools.getQuery('code')
  console.log('code', tools.getQuery('code'))
  window.sessionStorage.setItem('code', code)
  render()
}

export async function bootstrap () {
  console.log('[vue] vue app bootstraped')
}

export async function mount (props) {
  console.log('[vue] props from main framework', props)
  console.log(props)
  commonStore.globalRegister(store, props)
  Vue.prototype.$onGlobalStateChange = props.onGlobalStateChange
  Vue.prototype.$getGlobalState = props.getGlobalState
  Vue.prototype.$microTabsApi = props.microTabsApi
  store.dispatch('InitPortalInfo', props).then(() => {
    // 设置子应用路由规则
    store.dispatch('SET_MicroRulePath', props)
    render(props)
  })
}

export async function unmount () {
  console.log('[vue] vue app  unmount')
  instance.$destroy()
  instance.$el.innerHTML = ''
  instance = null
}
